package net.lab1024.sa.admin.module.business.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.exam.dao.CourseDao;
import net.lab1024.sa.admin.module.business.exam.dao.CourseaDao;
import net.lab1024.sa.admin.module.business.exam.dao.CoursebDao;
import net.lab1024.sa.admin.module.business.exam.dao.CoursecDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.CourseEntity;
import net.lab1024.sa.admin.module.business.exam.domain.entity.CourseaEntity;
import net.lab1024.sa.admin.module.business.exam.domain.entity.CoursebEntity;
import net.lab1024.sa.admin.module.business.exam.domain.entity.CoursecEntity;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.CourseAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.CourseQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.CourseUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.CourseVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程分类 Service
 *
 * @Author AKITA
 * @Date 2025-05-22 15:22:00
 * @Copyright AKITA
 */

@Service
public class CourseService {

    @Resource
    private CourseDao courseDao;
    @Resource
    private CourseaDao courseaDao;
    @Resource
    private CoursebDao coursebDao;
    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private CoursecDao coursecDao;

    /**
     * 分页查询
     */
    public PageResult<CourseVO> queryPage(CourseQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CourseVO> list = courseDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional
    public ResponseDTO<Long> add(CourseAddForm addForm) {
        CourseEntity courseEntity = SmartBeanUtil.copy(addForm, CourseEntity.class);
        courseDao.insert(courseEntity);
        for (CourseaEntity courseaEntity : addForm.getLista()) {
            courseaDao.insert(courseaEntity);
        }
        return ResponseDTO.ok(courseEntity.getDocEntry());
    }

    /**
     * 更新
     *
     */

    @Transactional
    public ResponseDTO<String> update(CourseUpdateForm updateForm) {
        CourseEntity courseEntity = SmartBeanUtil.copy(updateForm, CourseEntity.class);
        if (courseDao.updateById(courseEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        courseaDao.deleteById(updateForm.getDocEntry());
        for (CourseaEntity courseaEntity : updateForm.getLista()) {
            courseaDao.insert(courseaEntity);
        }
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        courseDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        courseDao.deleteById(docEntry);
        courseaDao.deleteById(docEntry);
        coursebDao.deleteById(docEntry);
        coursecDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<CourseEntity> openAll(Long docEntry) {
        List<EmployeeEntity> employeeEntities = employeeDao.selectList(null);
        int lineNum  = 0;
        int lineNumc = 0;
        CoursebEntity coursebEntity = new CoursebEntity();
        CoursecEntity coursecEntity = new CoursecEntity();


        for (EmployeeEntity employeeEntity : employeeEntities) {
            coursecEntity.setDocEntry(docEntry);
            coursecEntity.setLineNum(++lineNumc);
            coursecEntity.setUserId(employeeEntity.getEmployeeId());
            coursecEntity.setUserName(employeeEntity.getLoginName());
            coursecEntity.setLineStatus("O");
            coursecDao.insert(coursecEntity);
            for (CourseaEntity courseaEntity : courseaDao.selectList(new LambdaQueryWrapper<CourseaEntity>().eq(true, CourseaEntity::getDocEntry, docEntry))) {
                coursebEntity.setDocEntry(docEntry);
                coursebEntity.setLineNum(++lineNum);
                coursebEntity.setBaseEntry(docEntry);
                coursebEntity.setBaseLineNum(courseaEntity.getLineNum());
                coursebEntity.setTitle(courseaEntity.getTitle());
                coursebEntity.setFile(courseaEntity.getFile());
                coursebEntity.setType(courseaEntity.getType());
                coursebEntity.setUserId(employeeEntity.getEmployeeId());
                coursebEntity.setUserName(employeeEntity.getLoginName());
                coursebEntity.setLineStatus("O");
                coursebDao.insert(coursebEntity);
            }
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<List<CourseEntity>> myList(CourseQueryForm queryForm) {
        Long requestUserId = SmartRequestUtil.getRequestUserId();
        List<CourseEntity> entityList = courseDao.myList(requestUserId, queryForm.getLineStatus());
        return ResponseDTO.ok(entityList);
    }

    public ResponseDTO<CourseEntity> myInfo(Long docEntry) {
        Long requestUserId = SmartRequestUtil.getRequestUserId();
        CourseEntity courseEntity = courseDao.selectById(docEntry);
        courseEntity.setLista(courseaDao.selectList(new LambdaQueryWrapper<CourseaEntity>().eq(true, CourseaEntity::getDocEntry, docEntry)));
        courseEntity.setListb(coursebDao.selectList(new LambdaQueryWrapper<CoursebEntity>().eq(true, CoursebEntity::getDocEntry, docEntry).eq(CoursebEntity::getUserId, requestUserId)));
        courseEntity.setListc(coursecDao.selectList(new LambdaQueryWrapper<CoursecEntity>().eq(true, CoursecEntity::getDocEntry, docEntry).eq(CoursecEntity::getUserId, requestUserId)));
        return ResponseDTO.ok(courseEntity);
    }


    public ResponseDTO<CourseEntity> info(Long docEntry) {
        CourseEntity courseEntity = courseDao.selectById(docEntry);
        courseEntity.setLista(courseaDao.selectList(new LambdaQueryWrapper<CourseaEntity>().eq(true, CourseaEntity::getDocEntry, docEntry)));
        courseEntity.setListb(coursebDao.selectList(new LambdaQueryWrapper<CoursebEntity>().eq(true, CoursebEntity::getDocEntry, docEntry)));
        courseEntity.setListc(coursecDao.selectList(new LambdaQueryWrapper<CoursecEntity>().eq(true, CoursecEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(courseEntity);

    }
}
